# Melina Demertzi

#create and manipulate the user interface
# for the teacher client

import pango
import os
import gtk
import gtk.glade
import gtkmozembed
import XMLtoMindmap
import MindmaptoXML
import DrawDiagram

class teacher:
	def __init__(self, runaslib=True):
		# Load Glade XML
		self.xml = gtk.glade.XML("project2.glade")
		
		# Get Window
		self.w = self.xml.get_widget('window1')
		self.w.connect("delete_event", gtk.main_quit)
		
		# get one scrolled pane to put gecko in it
		self.url_pane = self.xml.get_widget('scrolledwindow1')
		self.port = self.url_pane.get_child()
		self.moz = gtkmozembed.MozEmbed()
		self.port.add(self.moz)
		self.moz.load_url("http://google.com")
		self.moz.show()

		# get second tool bar to put gecko in it
		self.mind_pane = self.xml.get_widget('scrolledwindow2')
                self.port2 = self.mind_pane.get_child()
                self.moz2 = gtkmozembed.MozEmbed()
                self.port2.add(self.moz2)
                self.moz2.load_url("file:///home/melina/Documents/misc/olpc/project/code/greece-india/star.png")
                self.moz2.show()
		
		#get new menu
		self.new_menu = self.xml.get_widget('button1')
		self.new_menu.connect('clicked',self.new_menu_listen)

		#get popup windows and their contents
		self.new_win = self.xml.get_widget('window5')
		self.new_file = self.xml.get_widget('entry10')
		self.new_button = self.xml.get_widget('button12')
		self.new_button.connect('clicked',self.new_listen)

		#get create node
		self.create_menu = self.xml.get_widget('button4')
		self.create_menu.connect('clicked',self.create_node_listen)

		self.create_win = self.xml.get_widget('window2')
		self.node_name = self.xml.get_widget('entry1')
		self.node_button = self.xml.get_widget('button6')
		self.node_button.connect('clicked', self.create_listen)

		#get create edge
		self.create_menu2 = self.xml.get_widget('button5')
		self.create_menu2.connect('clicked',self.create_edge_listen)

		self.create_win2 = self.xml.get_widget('window3')
		self.str_data = self.xml.get_widget('entry6')
		self.end_data = self.xml.get_widget('entry7')
		self.edge_button = self.xml.get_widget('button9')
		self.edge_button.connect('clicked',self.create_edge)

		#save mindmap
		self.save_button = self.xml.get_widget('button3')
		self.save_button.connect('clicked',self.save_mindmap)

		#open mindmap
		self.open_b = self.xml.get_widget('button2')
		self.open_b.connect('clicked',self.open_listen)

		self.open_win = self.xml.get_widget('window4')
		self.open_f = self.xml.get_widget('entry9')
		self.open_button = self.xml.get_widget('button11')
		self.open_button.connect('clicked',self.open_mindmap)

		self.nodes = []
		self.edges = []

		if not runaslib:
			self.w.show_all()
			self.new_win.hide_all()
			self.create_win.hide_all()
			self.create_win2.hide_all()
			self.open_win.hide_all()
			gtk.main()
	
	def new_menu_listen(self,*args):
		self.moz2.load_url("file:///home/melina/Documents/misc/olpc/project/code/greece-india/white.png")
		self.new_win.show_all()

	#listener when new is pressed
	def new_listen(self,*args):
		os.system("rm *dot")
		self.nodes = []
		self.edges = []

		self.new_win.hide_all()
		self.mindmap = self.new_file.get_text()
		print "creating "+self.mindmap

	def create_node_listen(self,*args):
		self.create_win.show_all()

	def create_listen(self,*args):
		self.create_win.hide_all()
		node_label = self.node_name.get_text()
		#add node to list
		self.nodes.append(node_label)
		#create svg and show on screen
		diagram = DrawDiagram.Diagram()
                diagram.GenDiagram(self.edges, self.nodes, True)
                diagram.WriteDiagram()
                fil = self.mindmap+".svg"
                diagram.Diagram2Svg(fil)
                fil2 = "file:///home/melina/Documents/misc/olpc/project/code/greece-india/"+fil
                self.moz2.load_url(fil2)
                self.moz2.show()

	def create_edge_listen(self,*args):
		self.create_win2.show_all()
	
	#add listener for creating edge choice
	def create_edge(self,*args):
		self.create_win2.hide_all()
		n1 = self.str_data.get_text()
		n2 = self.end_data.get_text()
		edge = [n1, n2]
		self.edges.append(edge)

		#re-create mindmap and draw
                diagram = DrawDiagram.Diagram()
                diagram.GenDiagram(self.edges, self.nodes, True)
                diagram.WriteDiagram()
                fil = self.mindmap+".svg"
                diagram.Diagram2Svg(fil)
                fil2 = "file:///home/melina/Documents/misc/olpc/project/code/greece-india/"+fil
                self.moz2.load_url(fil2)
                self.moz2.show()

	#save mindmap in xml format
	def save_mindmap(self,*args):
		#create xml
		fout = self.mindmap+".xml"
		mind = MindmaptoXML.XMLmindmap(self.mindmap, self.nodes, self.edges, fout)
		mind.create_xmlmindmap()
		mind.store_xmlmindmap()
		#cleanup intermediate files
		os.system("rm *dot")
		os.system("rm *svg")

	def open_listen(self,*args):
		self.open_win.show_all()

	def open_mindmap(self,*args):
		os.system("rm *dot")

		self.open_win.hide_all()
		self.input= self.open_f.get_text() 
		self.mindmap = self.input
		fopen = self.input+".xml"

		#re -create mind map
		mind = XMLtoMindmap.Mindmap(fopen)
		self.nodes= mind.get_mindmap_nodes()
		self.edges = mind.get_mindmap_edges()

		diagram = DrawDiagram.Diagram(self.input)
		diagram.GenDiagram(self.edges, self.nodes, True)
		diagram.WriteDiagram()
		fil = self.input+".svg"
		diagram.Diagram2Svg(fil)
		
		fil = "file:///home/melina/Documents/misc/olpc/project/code/greece-india/"+self.input+".svg"
		self.moz2.load_url(fil)
		self.moz2.show()

if __name__ == '__main__':
	teacher(False)


